//
//  MSEditPasswordVC.m
//  MS
//
//  Created by Yuan on 13-4-14.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSEditPasswordVC.h"

@interface MSEditPasswordVC ()

@end

@implementation MSEditPasswordVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)loadView
{
    [super loadView];

    NSString *title = @"";
    NSString *placeholder = @"";
    CGFloat offset_y = 0;
    for (int i = 0 ; i < 3; i++)
    {
        switch (i) {
            case 0:
                title = HYLocalizedString(@"原密码:");
                placeholder = HYLocalizedString(@"请输入原密码");
                break;
            case 1:
                title = HYLocalizedString(@"新密码:");
                placeholder = HYLocalizedString(@"请输入新密码");
                break;
            case 2:
                title = HYLocalizedString(@"确认新密码:");
                placeholder = HYLocalizedString(@"请再次输入新密码");
                break;
            default:
                break;
        }
        offset_y = 20 + 46*i;
        UIView *v = [self inputView:CGRectMake(10, offset_y, 300, 30) title:title];
        v.tag = i+1;
        [self.view addSubview:v];
        
        UITextField *txtField = (UITextField *)[v viewWithTag:100];
        [txtField setPlaceholder:placeholder];
    }
    
    UITextField *originPwdTxt = (UITextField *)[[self.view viewWithTag:1] viewWithTag:100];
    [originPwdTxt becomeFirstResponder];
}

-(UIView *)inputView:(CGRect )rect title:(NSString *)title
{
    UIView *v = [[UIView alloc]initWithFrame:rect];
    UILabel *titleLbl = [MSTool createLable:CGRectMake(0, 0, 85, 30)];
    [titleLbl setText:title];
    [titleLbl setTextAlignment:UITextAlignmentRight];
    [v addSubview:titleLbl];
    
    UITextField *field = [MSTool createTextField:CGRectMake(100, 0, 180, 30)];
    field.secureTextEntry = YES;
    field.tag = 100;
    [v addSubview:field];
    
    return v;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:HYLocalizedString(@"修改密码") showBackButton:YES];
    [self setRightItem:HYLocalizedString(@"确定")];
    // Do any additional setup after loading the view from its nib.
}

- (void)configureViews:(BOOL)animation
{
    [super configureViews:animation];
}

-(void)rightBarButtonItemAction:(UIButton *)btn
{
    UITextField *originPwdTxt = (UITextField *)[[self.view viewWithTag:1] viewWithTag:100];
    UITextField *newPwdTxt = (UITextField *)[[self.view viewWithTag:2] viewWithTag:100];
    UITextField *rNewPwdTxt = (UITextField *)[[self.view viewWithTag:3] viewWithTag:100];
    
    if ([newPwdTxt.text length]<1||[rNewPwdTxt.text length]<1||[originPwdTxt.text length]<1)
    {
        [MSTool showAlert:@"数据未填写完整！"];
        return;
    }
    
    if (![newPwdTxt.text isEqualToString:rNewPwdTxt.text])
    {
        [MSTool showAlert:@"填写的新密码不一致！"];
        return;
    }
    
    [originPwdTxt resignFirstResponder];
    [newPwdTxt resignFirstResponder];
    [rNewPwdTxt resignFirstResponder];
    
    NSDictionary *p = [NSDictionary dictionaryWithObjectsAndKeys:
                       [[MSSetting sharedSetting]getAccountID],@"uid",
                       originPwdTxt.text,@"oldpassword",
                       newPwdTxt.text,@"newpassword",
                       rNewPwdTxt.text,@"confirm_password",nil];

    [WebService  getRequestAction:APIUpdatePassword
                            param:p
                          success:^(NSDictionary *info,NSError *error)
     {
         if (error == nil)
         {
             HYLog(@"%@",info);             
             [MSTool showAlert:@"修改密码成功！"];
             [self.navigationController popViewControllerAnimated:YES];
         }
         else
         {
             [originPwdTxt becomeFirstResponder];
         }
         [MSTool showError:error];
     } failure:^(id info ,NSError *error){
         [MSTool showError:error];
     } progress:self.navigationController.view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
